<?php
include_once('common/header.php');	
if($AppUI->id > 0) {
	Vfriend_Function::redirect(HOST_VFRIEND);
}	
$strFirstName 		= $objRequest->getParam('pfirstname', '');
$strLastName 		= $objRequest->getParam('plastname', '');
$strEmail 			= $objRequest->getParam('premail', '');
$strPassword		= $objRequest->getParam('prpassword', '');
$strPasswordConfirm	= $objRequest->getParam('ppassword_confirm', '');
$intGender			= $objRequest->getParam('pgender', 0);
$intYear			= $objRequest->getParam('pyear', 0);
$intMonth			= $objRequest->getParam('pmonth', 0);
$intDay				= $objRequest->getParam('pday', 0);
$strCaptcha			= $objRequest->getParam('pcaptcha', '');
$intSrcType			= $objRequest->getParam('psrctype', 0);
$intSrcId			= (int)$objRequest->getParam('psrcid', 0);
$strRefer			= isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : HOST_VFRIEND;
$intType			= $objRequest->getParam('ptype',0);
$intError			= 0;
if($strFirstName == "" || $strLastName == "" || $strEmail == "" || $strPassword == "" || $strPasswordConfirm == "" || $intGender == 0 || $intYear == 0 || $intMonth == 0 || ($strCaptcha == "" && !FUNCTION_INTVITE) || (FUNCTION_INTVITE && $intSrcId==0))
	$intError = 1;
elseif(!Vfriend_Captcha_Image::validate($strCaptcha) && !FUNCTION_INTVITE) {
	$intError = 2;
}elseif(FUNCTION_INTVITE && $intSrcId==0) {
	$intError = 17;
}
if($intError == 0) {
	$objVfriendUser 	= new Vfriend_User();	
	if($intSrcId > 0) {		
		$objHex			= $objVfriendUser->getInfoByHexUserId($intSrcId);
		if($objHex->id == 0) {
			$intError	= 18;
		}
	}
	if($intError == 0) {
		$strBirthdate 		= Vfriend_Function::convertDateSys($intDay, $intMonth, $intYear);
		$strInsPassword		= md5($strPassword);
		$arrResult 			= $objVfriendUser->add($strEmail, $strInsPassword, $strBirthdate, $intGender, $strLastName, $strFirstName, $intSrcType, $intSrcId);
		if($arrResult['errCode'] == 0) {
			if(isset($_SESSION['uinfo'])) unset($_SESSION['uinfo']);
			if(isset($_SESSION['uemail'])) unset($_SESSION['uemail']);
			$intHexUserId 	= $arrResult['hexuserid'];
			$intUserId		= $arrResult['userid'];
			$strCode		= $arrResult['code'];
			$intExpireTime	= time() + (24*60*60*ACTIVE_EXPIRE_TIME);
			// send
			$this->view->strCode		= $strCode;
			$this->view->intHexUserId	= Vfriend_Function::base64E($intHexUserId);
			$this->view->intExpireTime	= Vfriend_Function::base64E($intExpireTime);
			$this->view->strEmail		= $strEmail;
			$this->view->strPassword	= $strPassword;	
			$strSubject		= INDEX_ACTIVE_SUB;
			$objView 		= Zend_Registry::get('objView'); 
			$strContent 	= $objView->fetch('email/register.tpl');
			$objMail		= new Vfriend_Mail();
			$intSend		= $objMail->send(EMAIL_NOREPLY, EMAIL_NOREPLY_NAME, $strEmail, $strSubject, $strContent);
			if($intSend)
				Vfriend_Function::redirect(LINK_INDEX_ACT_HELP . '?userid=' . $intHexUserId);
			else
				Vfriend_Function::redirect($strRefer . '?err=3');
		}
		else {
			if($intType == 1) {
				$_SESSION['uinfo'] = $objRequest->getParams();
			}
			$_SESSION['uemail']	= $strEmail;
			Vfriend_Function::setMsg($arrMessage[3]);
			Vfriend_Function::redirect($strRefer . '?err=-1');
		}
	}
}
if($intError > 0) {
	$_SESSION['uinfo'] = $objRequest->getParams();	
	$arrRefer	= explode('?err',$strRefer);
	$intCount	= count($arrRefer);
	if($intCount > 0) {
		$strLink = $arrRefer[0];
		Vfriend_Function::redirect($strLink . '?err=' . $intError);		
	} else {
		Vfriend_Function::redirect($strRefer . '?err=' . $intError);
	}
}
?>